package com.example.jh352160.surfingtv.search.customView;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.jh352160.surfingtv.R;

import java.util.ArrayList;
import java.util.List;

import static android.content.Context.MODE_PRIVATE;

/**
 * Created by jh352160 on 2016/8/1
 */

public class SearchEditview extends LinearLayout {

    Context context;
    List<String> strings;
    EditText searchEdit;

    public SearchEditview(Context context) {
        super(context);
        this.context = context;
        init();
    }

    public SearchEditview(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    private void init() {
        LayoutInflater.from(context).inflate(R.layout.search_editview, this);
        searchEdit = (EditText) findViewById(R.id.search_edit);
        final ImageView cleanButton = (ImageView) findViewById(R.id.search_clean);
        strings = new ArrayList<>();
        SharedPreferences preferences = context.getSharedPreferences("search_history", MODE_PRIVATE);
        for (int i = 0; i < 10; i++) {
            if (preferences.contains("string" + i)) {
                strings.add(preferences.getString("string" + i, ""));
            } else {
                break;
            }
        }
        searchEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (searchEdit.getText().toString().equals("")) {
                    cleanButton.setVisibility(INVISIBLE);
                } else {
                    cleanButton.setVisibility(VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        cleanButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                searchEdit.setText("");
            }
        });
//        Button searchButton=(Button)findViewById(R.id.search_button);
//        searchButton.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                strings.add(searchEdit.getText().toString());
//                if(strings.size()>10){
//                    strings.remove(10);
//                }
//                SharedPreferences.Editor editor = context.getSharedPreferences("search_history", MODE_PRIVATE).edit();
//                for (int i=0;i<strings.size();i++){
//                    editor.putString("string"+i,strings.get(i));
//                }
//                editor.apply();
//                //context.
//            }
//        });
    }

    public void search() {
        strings.add(searchEdit.getText().toString());
        if (strings.size() > 10) {
            strings.remove(10);
        }
        SharedPreferences.Editor editor = context.getSharedPreferences("search_history", MODE_PRIVATE).edit();
        for (int i = 0; i < strings.size(); i++) {
            editor.putString("string" + i, strings.get(i));
        }
        editor.apply();
    }

    public void cleanAll() {
        strings = new ArrayList<>();
    }
}
